package com.lxq.LeetCode.lengthOfLastWord;

public class Solution {
    public static void main(String[] args) {
        System.out.println(lengthOfLastWord(" aaaaa"));
    }

    public static int lengthOfLastWord(String s) {
        //法一：

        int i, j = -1;
        for (i = s.length() - 1; i >= 0; i--) {
            if (s.charAt(i) != ' ' && j == -1) {
                j = i;
            } else if (s.charAt(i) == ' ' && j != -1) {
                break;
            }
        }
        return j - i;
        //法二：
//        int res = 0;
//        String[] sArray = s.split(" ");
//        res = sArray[sArray.length - 1].length();
//        return res;
    }
}
